﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PlanningGIS.Win.Controls;
using PlanningGIS.Framework.Rule;

namespace PlanningGIS.Framework.Controls
{
    /// <summary>
    /// 保存规则窗体，可供用户输入一个新的规则名，或选择一个旧的进行覆盖
    /// </summary>
    public partial class FrmSaveRule : Office2007Form
    {
        private string ruleName = "";
        private zjgisRuleType ruleType;
        private object obj;

        /// <summary>
        /// 如果是在已有的规则上进行保存，则需传入该规则名称
        /// </summary>
        public string RuleName
        {
            set { ruleName = value; }
        }

        /// <summary>
        /// 设置名称后，传出的对象。若是新规则，则传出规则名称（string类型）；若是需要在已有规则上保存，则传出该规则对象（RuleXML类型）
        /// </summary>
        public object Rule
        {
            get { return obj; }
        }

        public FrmSaveRule(zjgisRuleType value)
        {
            InitializeComponent();
            ruleType = value;
        }

        private void FrmSaveRule_Load(object sender, EventArgs e)
        {
            this.cboRuleName.Items.Clear();

            //switch (ruleType)
            //{
            //    case zjgisRuleType.dlgImpByCode :
            //        List<DataImportRule> dataImportRules = RuleConfig.rulesXmlContainer.BuildingRules;
            //        foreach (DataImportRule rule in dataImportRules)
            //        {
            //            this.cboRuleName.Items.Add(rule);
            //        }
            //        break;

            //    case zjgisRuleType.dlgImpByLyr :
            //        //List<DataMatchRule> dataMatchRules = RuleConfig.rulesXmlContainer.dataMatchtRules;
            //        //foreach (DataMatchRule rule in dataMatchRules)
            //        //{
            //        //    this.cboRuleName.Items.Add(rule);
            //        //}
            //        break;

            //    case zjgisRuleType.dlgExp :
            //        //List<DataExportRule> dataExportRules = RuleConfig.rulesXmlContainer.dataExportRules;
            //        //foreach (DataExportRule rule in dataExportRules)
            //        //{
            //        //    this.cboRuleName.Items.Add(rule);
            //        //}
            //        break;

            //    case zjgisRuleType.dlgMerge :
            //        List<MergeRule> mergeRules = RuleConfig.rulesXmlContainer.mergeRules;
            //        foreach (MergeRule rule in mergeRules)
            //        {
            //            this.cboRuleName.Items.Add(rule);
            //        }
            //        break;

            //    case zjgisRuleType.dlgCheck :
            //        //List<DataCheckRule> dataCheckRules = RuleConfig.rulesXmlContainer.dataCheckRules;
            //        //foreach (DataCheckRule rule in dataCheckRules)
            //        //{
            //        //    this.cboRuleName.Items.Add(rule);
            //        //}
            //        break;

            //    case zjgisRuleType.pipCode :
            //        List<PipeLayerCodeRule> pipeLayerCodeRules = RuleConfig.rulesXmlContainer.pipeLayerRules;
            //        foreach (PipeLayerCodeRule rule in pipeLayerCodeRules)
            //        {
            //            this.cboRuleName.Items.Add(rule);
            //        }
            //        break;

            //    case zjgisRuleType.pipImp:
            //        List<PipeImportRule> pipeImportRules = RuleConfig.rulesXmlContainer.pipeImportRules;
            //        foreach (PipeImportRule rule in pipeImportRules)
            //        {
            //            this.cboRuleName.Items.Add(rule);
            //        }
            //        break;

            //    case zjgisRuleType.pnCheck :
            //        List<PNDataCheckRule> PNDataCheckRules = RuleConfig.rulesXmlContainer.pndataCheckRules;
            //        foreach (PNDataCheckRule rule in PNDataCheckRules)
            //        {
            //            this.cboRuleName.Items.Add(rule);
            //        }
            //        break;
            //}

            if (ruleName != "") this.cboRuleName.Text = ruleName;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (cboRuleName.Text.Trim() == "")
            {
                MsgHelper.ShowInfomationMsgbox("请设置规则名称！");
                return;
            }
            string name = cboRuleName.Text.Trim();
            foreach (object item in cboRuleName.Items)
            {
                if (item is RuleXml)
                {
                    RuleXml rule = item as RuleXml;
                    if (rule.RuleName == name)
                    {
                        if (MsgHelper.ShowYesOrNoQueryMsgBox("规则[" + name + "]已经存在，是否需要进行覆盖？") == DialogResult.No)
                        {
                            return;
                        }
                        obj = rule;
                        break;
                    }                }
            }
            if (obj == null) obj = name;
            DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
